<template>
  <div class="app-container">
    <!--搜索栏 开始-->
<!--    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="商品名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入商品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">刷新</el-button>
      </el-form-item>
    </el-form>-->
    <!--搜索栏 结束-->

    <!--操作栏 开始-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:post:add']"
        >新增
        </el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:post:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:post:remove']"
        >删除
        </el-button>
      </el-col>-->
    </el-row>
    <!--操作栏 结束-->
    <!--
     <el-table  v-loading="loading" :data="tableData" @selection-change="handleSelectionChange">
     <el-table-column  type="selection" width="55" align="center">
     -->

    <el-table v-loading="loading" :data="tableData">
      <el-table-column
        sortable
        align="center"
        label="轮播图"
      >
        <template slot-scope="scope">
          <!-- scope.row 包含表格里当前行的所有数据 -->
          <img :src="imgPrex+scope.row.bannerUrl" style="width: 100px;height: 56px;">
        </template>
      </el-table-column>
      <el-table-column
        sortable
        align="center"
        label="跳转类型"
      >
        <template slot-scope="scope">
          <!-- scope.row 包含表格里当前行的所有数据 -->
          <span v-if="scope.row.jumpType == 0">商品资源</span>
          <span v-if="scope.row.jumpType == 1">文章资源</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="sourceId"
        label="资源id"
      >
      </el-table-column>
      <el-table-column
        sortable
        align="center"
        prop="sourceName"
        label="资源名称"
      >
      </el-table-column>

<!--      <el-table-column
        sortable
        align="center"
        prop="createUser"
        label="创建人"
      >
      </el-table-column>
      <el-table-column
        sortable
        align="center"
        prop="createUser"
        label="创建时间"
      >
      </el-table-column>-->
      <el-table-column
        sortable
        align="center"
        prop="updateUser"
        label="修改人"
      >
      </el-table-column>
      <el-table-column
        sortable
        align="center"
        prop="updateTime"
        label="修改时间"
      >
      </el-table-column>
      <el-table-column
        sortable
        align="center"
        prop="sort"
        label="排序"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="状态">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <!--      <el-table-column
              sortable
              align="center"
              prop="info"
              label="商品说明">
            </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:post:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:post:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>

import {listMiniBanner, delMiniBanner, updateMiniBanner} from '@/api/mini/banner'
import config from '@/utils/config'
import {updateMiniWenzhang} from "@/api/mini/wenzhang";

export default {
  name: 'MiniBanner',
  data() {
    return {
      imgPrex: config.imgPrefx,
      loading: true,
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      total: 0,
      tableData: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      form: {
        id: '',
        name: '',
        pic: '',
        nowPrice: '',
        type: '',
        info: '',
        createBy: '',
        createTime: '',
        updateBy: '',
        updateTime: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      this.resetForm()
      listMiniBanner(this.queryParams).then(response => {
        this.tableData = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    cancel() {
      this.open = false
      this.resetForm()
    },
    resetForm() {
      this.form = {}
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 启用，停用 */
    handleStatusChange(row) {
      let text = row.status === '0' ? '启用' : '停用'
      this.$modal.confirm('确认要' + text + "该记录吗？").then(function() {
        return updateMiniBanner({
          "id":row.id,
          "status":row.status
        })
      }).then(() => {
        this.$modal.msgSuccess(text + '成功')
      }).catch(function() {
        row.status = row.status === '0' ? '1' : '0'
      })
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({ name: 'BannerModify' })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const keyId = row.id || this.ids
      this.$router.push({
        name: 'BannerModify',
        params: {
          keyId: keyId
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const keyIds = row.id || this.ids
      this.$confirm('是否确认删除编号为"' + keyIds + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delMiniBanner(keyIds)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(function() {
      })
    }
  }
}
</script>
